Learn R Programming

ChainLadder (version 0.2.15)

triangle S3 Methods: Generic functions for triangles

Description

Functions to ease the work with triangle shaped matrix data. A 'triangle' is a matrix with some generic functions.

triangle creates a triangle from the given set of vectors of observed data.

as.triangle attempts to turn its argument into a triangle. Triangles are usually stored in a “long” format in data bases. The function can transform a data.frame into a triangle shape.

as.data.frame turns a triangle into a data frame.

Usage

triangle(..., bycol=FALSE, origin="origin", dev="dev", value="value")

# S3 method for matrix as.triangle(Triangle, origin="origin", dev="dev", value="value", ...) # S3 method for data.frame as.triangle(Triangle, origin="origin", dev="dev", value="value", ...) # S3 method for triangle as.data.frame(x, row.names=NULL, optional, lob=NULL, na.rm=FALSE, ...) as.triangle(Triangle, origin="origin", dev="dev", value="value", ...) # S3 method for triangle plot(x, type = "b", xlab = "dev. period", ylab = NULL, lattice=FALSE, ...)

Arguments

Triangle

a triangle

bycol

logical. If FALSE (the default) the triangle is filled by rows, otherwise the triangle is filled by columns.

origin

name of the origin period, default is "origin".

dev

name of the development period, default is "dev".

value

name of the value, default is "value".

row.names

default is set to NULL and will merge origin and dev. period to create row names.

lob

default is NULL. The idea is to use lob (line of business) as an additional column to label a triangle in a long format, see the examples for more details.

optional

not used

na.rm

logical. Remove missing values?

x

a matrix of class 'triangle'

xlab

a label for the x axis, defaults to 'dev. period'

ylab

a label for the y axis, defaults to NULL

lattice

logical. If FALSE the function matplot is used to plot the developments of the triangle in one graph, otherwise the xyplot function of the lattice package is used, to plot developments of each origin period in a different panel.

type

type, see plot.default

...

vectors of data in triangle, see details; arguments to be passed to other methods everywhere else.

Author

Markus Gesmann, Dan Murphy, Vincent Goulet

Warning

Please note that for the function as.triangle the origin and dev. period columns have to be of type numeric or a character which can be converted into numeric.

Also note that when converting from a data.frame to a matrix with as.triangle, multiple records with the same origin and dev will be aggregated.

Details

Function triangle builds a triangle matrix from the vectors of known data provided in .... Normally, each of these vectors should be one shorter than the preceeding one. The length of the first vector dictates the number of development periods or origin periods (respectively when bycol is FALSE or TRUE). As a special case, the function will build an \(n \times n\) triangle from a single vector of \(n(n + 1)/2\) data points.

The names of the arguments in ... for function triangle (when there are more than one) are retained for row/column names. Similarly, the names of the elements of the first argument are used as column/row names.

Examples

Run this code
GenIns
plot(GenIns)
plot(GenIns, lattice=TRUE)


## Convert long format into triangle
## Triangles are usually stored as 'long' tables in data bases
head(GenInsLong)
as.triangle(GenInsLong, origin="accyear", dev="devyear", "incurred claims")

X <- as.data.frame(RAA)
head(X)

Y <- as.data.frame(RAA, lob="General Liability")
head(Y)

## Basic creation of a triangle from loss development data
triangle(c(100, 150, 175, 180, 200),
         c(110, 168, 192, 205),
         c(115, 169, 202),
         c(125, 185),
         150)

## Same, with named origin periods
triangle("2012" = c(100, 150, 175, 180, 200),
         "2013" = c(110, 168, 192, 205),
         "2014" = c(115, 169, 202),
         "2015" = c(125, 185),
         "2016" = 150)

## Again, with also named development periods
triangle("2012" = c("12 months" = 100,
                    "24 months" = 150,
                    "36 months" = 175,
                    "48 months" = 180,
                    "60 months" = 200),
         "2013" = c(110, 168, 192, 205),
         "2014" = c(115, 169, 202),
         "2015" = c(125, 185),
         "2016" = 150)

## Quick, simplified usage
triangle(c(100, 150, 175, 110, 168, 115))

Run the code above in your browser using DataLab